<!--
 * @author  tanyuze
 * @desc    新建/编辑完成条件
-->
<template>
  <div class="escrow-created">
    <div class="escrow-created-title">新建/编辑荣誉完成条件</div>
    <section v-loading="loading" class="escrow-created-main">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px" class="form">
        <el-form-item label="名称" prop="seriesName">
          <el-input v-model.trim="form.seriesName" placeholder="请输入名称" maxlength="50" />
        </el-form-item>
        <el-form-item label="配置类型" prop="seriesType">
          <el-select v-model="form.seriesType" placeholder="请选择配置类型" clearable :style="{width:'100%'}">
            <el-option v-for="(item, index) in dict.RYTJ" :key="index" :label="item.dictName" :value="item.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="说明" prop="seriesExplain">
          <el-input v-model="form.seriesExplain" :rows="6" type="textarea" placeholder="请输入说明" maxlength="300" />
        </el-form-item>
        <!--        <el-form-item label="是否有有效期" prop="isHaveTime">
          <el-radio-group v-model="form.isHaveTime">
            <el-radio :label="0">没有</el-radio>
            <el-radio :label="1">有</el-radio>
          </el-radio-group>
        </el-form-item>-->
        <el-form-item v-if="form.isHaveTime === 1" label="有效开始时间" prop="validityStart">
          <el-date-picker
            v-model="form.validityStart"
            style="width: 100%"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择"
          />
        </el-form-item>
        <el-form-item v-if="form.isHaveTime === 1" label="有效结束时间" prop="validityEnd">
          <el-date-picker
            v-model="form.validityEnd"
            style="width: 100%"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择"
          />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model.number="form.sort" placeholder="请输入排序号（值越大，越靠前）" maxlength="5" />
        </el-form-item>
      </el-form>
    </section>
    <section class="escrow-created-footer">
      <el-button style="padding: 10px 30px;" @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="onSubmit('form')">确 定</el-button>
    </section>
  </div>
</template>

<script>
import { number, specialText } from '@/utils/validate'
import { getDictByCode, honorSeriesInsert, honorSeriesUpdate, honorSeriesGet } from '../api'
import elDragDialog from '@/directive/el-drag-dialog'
export default {
  name: 'EscrowManage',
  directives: { elDragDialog },
  filters: {},
  data() {
    return {
      loading: false,
      form: {
        id: '',
        seriesName: '',
        seriesType: '',
        seriesExplain: '',
        isHaveTime: 0,
        validityStart: '',
        validityEnd: '',
        sort: ''
      },
      rules: {
        seriesName: [
          { required: true, message: '请输入名称', trigger: 'blur' }, { validator: specialText, trigger: 'blur' }
        ],
        seriesType: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ],
        sort: [
          { required: true, message: '请输入排序', trigger: 'blur' }, { validator: number, trigger: 'blur' }
        ]
      },
      state: 'create',
      dict: {
        RYTJ: []
      }
    }
  },
  created() {
    this.state = this.$route.query.type === 'u' ? 'update' : 'create'
    if (this.$route.query.type === 'u') {
      if (!this.$route.query.i) {
        this.$message({
          showClose: true,
          message: '参数异常！',
          type: 'warning',
          onClose: () => {
            this.handleCancel()
          }
        })
      }
      this.form.id = this.$route.query.i
      this.getData()
    }
    for (const key in this.dict) {
      this.getDict(key)
    }
  },
  methods: {
    /**
     * 字典
     */
    getDict(code) {
      return new Promise((resolve, reject) => {
        getDictByCode(code)
          .then((response) => {
            this.dict[code] = response.data.data[0].list
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    /**
     * 返回
     */
    handleCancel() {
      this.$router.replace({ path: this.$filterPath('escrow'), query: { }})
    },
    onSubmit() {
      if (this.state === 'create') {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            const loading = this.$loading({
              lock: true,
              text: '请稍后',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.3)'
            })
            const params = Object.assign({}, this.form)
            if (this.form.isHaveTime === 0) {
              delete params.validityStart
              delete params.validityEnd
            }
            return new Promise((resolve, reject) => {
              honorSeriesInsert(params).then(() => {
                loading.close()
                this.$message({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 2000
                })
                this.handleCancel()
              }).catch(error => {
                reject(error)
                loading.close()
              })
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      } else {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            const loading = this.$loading({
              lock: true,
              text: '请稍后',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.3)'
            })
            const params = Object.assign({}, this.form)
            if (this.form.isHaveTime === 0) {
              delete params.validityStart
              delete params.validityEnd
            }
            return new Promise((resolve, reject) => {
              honorSeriesUpdate(params).then(() => {
                loading.close()
                this.$message({
                  title: '成功',
                  message: '修改成功',
                  type: 'success',
                  duration: 2000
                })
                this.handleCancel()
              }).catch(error => {
                reject(error)
                loading.close()
              })
            })
          } else {
            return false
          }
        })
      }
    },
    /**
     * 根编辑回显
     */
    getData() {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      return new Promise((resolve, reject) => {
        honorSeriesGet({ id: this.form.id }).then(res => {
          loading.close()
          const data = res.data.data
          for (const key in this.form) {
            this.form[key] = data[key]
          }
        }).catch(error => {
          loading.close()
          reject(error)
        })
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">

.escrow-created {
  background-color: #fff;
  min-height: calc(100vh - 60px);
  max-height: calc(100vh - 60px);
  overflow: hidden;
  .escrow-created-title {
    position: relative;
    height: 40px;
    line-height: 40px;
    padding: 0 20px;
    background-color: #ffffff;
    border-bottom: 1px solid #e5e5e5;
    font-family: PingFangSC-Medium;
    font-weight: bold;
    color: rgba(51, 51, 51, 1);
    font-size: 14px;
    &:before {
      content: '';
      position: absolute;
      width: 2px;
      @include bg-color($mainColor);
      top: 13px;
      bottom: 13px;
      left: 15px;
    }
  }

  .escrow-created-main {
    position: absolute;
    top: 50px;
    left: 0;
    right: 0;
    bottom: 52px;
    box-sizing: border-box;
    padding: 0 10px;
    overflow: hidden;
    &::-webkit-scrollbar {
      width: 8px; /*对垂直流动条有效*/
      height: 1px; /*对水平流动条有效*/
    }
    /** 滚动条轨道*/
    &::-webkit-scrollbar-track {
      box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.1);
      background-color: white;
      border-radius: 3px;
    }
    /** 滚动条里面小方块*/
    &::-webkit-scrollbar-thumb {
      height: 32px;
      border-radius: 5px;
      box-shadow: inset 0 0 4px rgba(0, 0, 0, 0.1);
      background-color: #eaeaea;
    }
    &:hover {
      overflow: auto;
    }
    .form {
      width: 1000px !important;
      margin: 0 auto;
      .el-select {
        width: 100%;
      }

      .line {
        text-align: center;
      }
    }
    .select-data {
      display: inline-block;
      background: rgba(246, 246, 246, 1);
      border-radius: 4px;
      font-size: 14px;
      font-family: PingFangSC-Regular;
      font-weight: 400;
      color: rgba(51, 51, 51, 1);
      padding: 0 10px;
      margin-right: 5px;
      margin-bottom: 8px;
      i {
        cursor: pointer;
        padding-left: 5px;
      }
    }
    .selectbtn {
      display: inline-block;
      width: 120px;
      background: rgba(255, 255, 255, 1);
      @include border-color(1px, dotted, $mainColor);
      border-radius: 4px;
      font-size: 14px;
      font-family: PingFangSC-Regular;
      font-weight: 400;
      @include font-color($mainColor);
      text-align: center;
      cursor: pointer;
      margin-bottom: 10px;
    }
    .news-image {
      .el-card__header {
        padding-top: 8px;
        padding-bottom: 8px;
      }
    }
    .images-item {
      position: relative;
      margin-bottom: 10px;
      .upload-img {
        width: 100%;
        height: 160px;
        display: block;
      }
      .el-input {
        display: block;
      }
      .custom-uploader {
        width: 100%;
        display: block;
        .icon-wrap {
          width: 100%;
          height: 160px;
        }
      }
      .el-button {
        position: absolute;
        right: 10px;
        top: 0;
        color: #fff;
        i {
          background: rgba(0, 0, 0, 0.4);
          border-radius: 50%;
          padding: 2px;
        }
        &.image-index-move {
          right: 40px;
        }
      }
    }
    .images-add {
      color: $mainColor;
      font-size: 14px;
      padding-left: 80px;
      cursor: pointer;
      margin-bottom: 20px;
    }
    .upload-img {
      width: 100%;
      height: 160px;
    }
    .attachment-item {
      margin-bottom: 10px;
      .item-btn {
        font-size: 20px;
        cursor: pointer;
      }
      .video-item {
        position: relative;
        cursor: pointer;
        .el-button {
          position: absolute;
          right: 10px;
          top: 0;
          color: #fff;
          i {
            background: rgba(0, 0, 0, 0.4);
            border-radius: 50%;
            padding: 2px;
          }
          &.video-index-move {
            right: 40px;
          }
        }
      }
    }
  }

  .escrow-created-footer {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    text-align: center;
    padding: 10px 0;
    background-color: white;
    box-shadow: 0 -3px 6px #f5f5f5;
  }
  .image-upload-dialog {
    .icon-wrap {
      width: 300px;
      height: 160px;
      margin: 0 auto;
      display: flex;
      align-items: center;
      justify-content: center;
      background: rgba(247, 247, 248, 1);
      border: 1px solid rgba(226, 227, 232, 1);
      border-radius: 4px;
      .avatar-uploader-icon {
        font-size: 48px;
        color: #dadada;
        width: 300px;
        text-align: center;
      }
    }
    .upload-img {
      display: block;
      width: 300px;
      height: 160px;
      border-radius: 4px;
    }
  }
}
</style>
